package mod

import (
	"fmt"
	"service/conf"

	"github.com/qiniu/api.v7/v7/auth"
	"github.com/qiniu/api.v7/v7/auth/qbox"
	"github.com/qiniu/api.v7/v7/storage"
)

var Qiniu MQiniu

type MQiniu struct{}

// MQiniu.QiniuToken 七牛云token
func (this *MQiniu) QiniuToken() (string, error) {
	putPolicy := storage.PutPolicy{
		Scope: conf.Qiniu.Stg().Param.Bucket,
	}
	mac := auth.New(conf.Qiniu.Stg().Param.AccessKey, conf.Qiniu.Stg().Param.SecretKey)
	upToken := putPolicy.UploadToken(mac)
	return upToken, nil
}

// MQiniu.QinNiuDelete 七牛云删除
func (this *MQiniu) QinNiuDelete(fileName string) (string, error) {
	mac := qbox.NewMac(conf.Qiniu.Stg().Param.AccessKey, conf.Qiniu.Stg().Param.SecretKey)
	cfg := storage.Config{}
	// 空间对应的机房
	cfg.Zone = &storage.ZoneHuadong
	// 是否使用https域名
	cfg.UseHTTPS = false
	// 上传是否使用CDN上传加速
	cfg.UseCdnDomains = false
	// 构建表单上传的对象
	fmt.Println(conf.Qiniu.Stg().Param.AccessKey, conf.Qiniu.Stg().Param.SecretKey, conf.Qiniu.Stg().Param.Bucket, fileName)
	bucketManager := storage.NewBucketManager(mac, &cfg)
	key := fileName
	err := bucketManager.Delete(conf.Qiniu.Stg().Param.Bucket, key)
	if err != nil {
		fmt.Println(err)
		return "", nil
	}
	bu := "空间下文件删除"
	return bu, nil
}
